﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

namespace EQual.MainModule
{
   [ValueConversion(typeof(System .Data .Linq .Binary ), typeof(System.Windows.Media .ImageSource ))]
   public class BinaryToImageConverter:IValueConverter 
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                string personImage = "/Resources/person.png";
                Uri uri = new Uri(personImage,  UriKind.RelativeOrAbsolute );
                BitmapImage source = new BitmapImage(uri);
                return source;
            }

            byte[] imageArr =  ((System .Data .Linq .Binary ) value).ToArray () ;

            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.CreateOptions = BitmapCreateOptions.None;
            bi.CacheOption = BitmapCacheOption.Default;
            bi.StreamSource = new MemoryStream(imageArr);
            bi.EndInit();
            return bi;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
